//
//  ByFillCodeVC.m
//  Homept_ios
//
//  Created by mc on 16/9/2.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByFillCodeVC.h"
#import "ByResetPwdViewController.h"    //重置密码
#import "ByLoginViewModel.h"
#import "ByResetPwdViewController.h"

@interface ByFillCodeVC ()<UITextFieldDelegate,FetchCodeDelegate,CheckCodeDelegate>
{
    NSInteger _countdownTime;   // 倒计时剩余时间
    NSAttributedString *_title; // 可以点击获取验证码时的标题
    UIButton *_sendBtn;
    ByLoginViewModel *_viewmodel;
}

@end

@implementation ByFillCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"填写验证码";
    [self addLeftBackBarButtonItem];
    [self createView];
    _viewmodel = [[ByLoginViewModel alloc] init];
    _viewmodel.fetchcodeDelegate = self;
    
    
    //测试
    [self performSelector:@selector(showcode) withObject:nil afterDelay:0.01];
   
}

- (void)showcode {
    [ByCoreToolAlert showOnlyText:_passcode];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // 再次获取验证码定时器倒计时60秒
    _countdownTime = 60;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(_countdownAction:) userInfo:nil repeats:YES];
    [timer fire];

}
#pragma mark ---创建视图
- (void)createView{

    CGFloat x,y,width,height;
    x = 0;
    y = 30;
    width = SCREEN_WIDTH;
    height = 20;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.text = @"验证码已经发送到手机";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT(13);
    [self.view addSubview:titleLabel];
    
    //手机号
    y = titleLabel.bottom + 10;
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    phoneLabel.text = _phoneNumber;
    phoneLabel.font = FONT(13);
    phoneLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:phoneLabel];
    
    CGFloat margin = 5;
    //计算一个字符的宽度
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    lable.text = @"2";
    lable.font = FONT(24);
    CGFloat fontWidth = [lable.text getSizeOfString:24].width;
    //field
    y = phoneLabel.bottom + 40;
    x = (SCREEN_WIDTH - 6*fontWidth - 5*margin - 6*25)/2;
    width = SCREEN_WIDTH - 2*x;
    height = 40;
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(x +20, y, width, height)];
    field.keyboardType = UIKeyboardTypeNumberPad;
    field.tag = 111;
    field.font = FONT(24);
    [self.view addSubview:field];
    [field becomeFirstResponder];
    field.tintColor = [UIColor clearColor];
    [field addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    //循环创建线
    for (int i = 0; i<6; i++) {
        margin = 5; //字符间距
        width = fontWidth + 20;
        x = field.left - 10 +(width + margin)*i;
        y = field.bottom;
        height = 1;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        line.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
        [self.view addSubview:line];
    }
    //重新发送按钮
    x = field.left - 25;
    y = field.bottom + 30;
    width = SCREEN_WIDTH - 2*x;
    height = 20;
    _sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [_sendBtn setTitle:@"60秒后重新发送" forState:UIControlStateNormal];
    [_sendBtn setTitleColor:HTB_THEME_COLOR forState:UIControlStateNormal];
    [self.view addSubview:_sendBtn];
    [_sendBtn addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //label
    x = 0;
    y = _sendBtn.bottom + 20;
    width = SCREEN_WIDTH;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    label.font = FONT(14);
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"若无法收到验证码，请拨打：400-830-000";
    [self.view addSubview:label];
    
}
#pragma mark - 获取验证码
// 发送获取验证码请求，并进行下一次按钮允许点击倒计时，防止按钮点击过频
- (void)getCaptchaWithMobile:(NSString *)mobile type:(NSNumber *)type captchaBlock:(void(^)(NSString *))captchaBlock {
    // 禁止点击
    _sendBtn.enabled = NO;
}
#pragma mark - 倒计时处理
- (void)_countdownAction:(NSTimer *)timer {
    if (_countdownTime > 0) {
        _sendBtn.enabled = NO;
        _countdownTime --;
        [_sendBtn setTitle:[NSString stringWithFormat:@"%li秒后重新发送",_countdownTime] forState:UIControlStateNormal];
    }
    else {
        [_sendBtn setTitle:@"重新发送" forState:UIControlStateNormal];
        _sendBtn.enabled = YES;
        [timer invalidate];
    }
}

#pragma mark ---发送
- (void)sendAction:(UIButton *)btn{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.mobile = _phoneNumber;
    model.type = _type;
    [_viewmodel fetchCode:model];
    UITextField *field = [self.view viewWithTag:111];
    field.text = nil;
}
- (void)textFieldDidChange:(UITextField *)textField{
    UITextField *field = [self.view viewWithTag:111];
    NSDictionary *attrsDictionary =@{
                                     NSFontAttributeName: field.font,
                                     NSKernAttributeName:[NSNumber numberWithFloat:25.0f]//这里修改字符间距
                                     };
    field.attributedText=[[NSAttributedString alloc]initWithString:field.text attributes:attrsDictionary];
    if (field.text.length == 6) {
        _viewmodel.checkcodeDelegate = self;
        ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
        model.mobile = _phoneNumber;
        model.code = field.text;
        model.type = _type;
        [_viewmodel checkCode:model];
        
    }
}
#pragma mark -----相关代理
//发送验证码
- (void)fetchCodeSuccess:(ByNetBaseResult *)result{
    [ByCoreToolAlert showOnlyText:result.data];
}
- (void)fetchCodeFail:(ByNetBaseResult *)result{
    [ByCoreToolAlert showFailureWithText:result.msg];
}
//验证验证码
- (void)checkCodeSuccess:(ByNetBaseResult *) result{
    UITextField *field = [self.view viewWithTag:111];
    ByResetPwdViewController *vc = [[ByResetPwdViewController alloc] init];
    vc.type = _type;
    vc.phoneNumber = _phoneNumber;
    vc.code = field.text;
    [self by_normalPush:vc hasNavi:YES];
}
- (void)checkCodeFail:(ByNetBaseResult *)result{
    [ByCoreToolAlert showFailureWithText:result.msg];
}
@end
